package cz.muni.fi.pa165.travelagency.services;

import cz.muni.fi.pa165.travelagency.dto.OrderDTO;
import cz.muni.fi.pa165.travelagency.entities.Order;
import cz.muni.fi.pa165.travelagency.persistence.IOrderDAO;
import java.util.ArrayList;
import java.util.List;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Implementation of Order service
 *
 * @author Marián Mozola
 */
@Service
@Transactional
public class OrderService implements IOrderService {

    private Mapper mapper;
    private IOrderDAO orderDAO;

    @Autowired
    public void setMapper(Mapper mapper) {
        this.mapper = mapper;
    }

    @Autowired
    public void setOrderDAO(IOrderDAO orderDAO) {
        this.orderDAO = orderDAO;
    }

    @Override
    public void createOrder(OrderDTO order) {
        orderDAO.createOrder(mapper.map(order, Order.class));
    }

    @Override
    public void deleteOrder(OrderDTO order) {
        orderDAO.deleteOrder(mapper.map(order, Order.class));
    }

    @Override
    public List<OrderDTO> findAll() {
        List<Order> orders = orderDAO.findAll();
        List<OrderDTO> result = new ArrayList<>();
        for (Order order : orders) {
            result.add(mapper.map(order, OrderDTO.class));
        }
        return result;
    }

    @Override
    public List<OrderDTO> findUnpaid() {
        List<Order> orders = orderDAO.findUnpaid();
        List<OrderDTO> result = new ArrayList<>();
        for (Order order : orders) {
            result.add(mapper.map(order, OrderDTO.class));
        }
        return result;
    }

    @Override
    public OrderDTO getOrder(Long id) {
        Order order = orderDAO.getOrder(id);
        return order == null ? null : mapper.map(order, OrderDTO.class);
    }

    @Override
    public void updateOrder(OrderDTO order) {
        orderDAO.updateOrder(mapper.map(order, Order.class));
    }
}
